package com.jabo.douban.demo.presentation.view.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jabo.douban.demo.R;
import com.jabo.douban.demo.model.entity.NewsBean;
import com.jabo.douban.demo.test.NineGridPictureLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @anthor JZheng3
 * @Time: 2020/2/28
 */
public class RecommendRecyclerViewAdapter extends BaseRecyclerViewAdapter {

    private List<NewsBean.NewslistBean> mNewslistBeans;
    private List<String> mUrls = new ArrayList<>();

    public RecommendRecyclerViewAdapter() {
        mNewslistBeans = new ArrayList<>();
    }


    public void addDatas(List<NewsBean.NewslistBean> beans) {
        if (beans == null || beans.isEmpty()) {
            return;
        }
        mNewslistBeans.addAll(beans);
        notifyDataSetChanged();
    }

    public void setDatas(List<NewsBean.NewslistBean> beans) {
        if (beans == null || beans.isEmpty()) {
            return;
        }
        mNewslistBeans.clear();
        mNewslistBeans.addAll(beans);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_recommend, parent, false);
        return new RecommendedViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final NewsBean.NewslistBean newslistBean = mNewslistBeans.get(position);
        final RecommendedViewHolder viewHolder = (RecommendedViewHolder) holder;
        viewHolder.tvName.setText(newslistBean.getDescription());
        viewHolder.tvContent.setText(newslistBean.getTitle());
        viewHolder.tvTopic.setText(newslistBean.getCtime());
        Glide.with(viewHolder.ivAvatarUrl).load(newslistBean.getPicUrl()).into(viewHolder.ivAvatarUrl);

        mUrls.clear();
        for (int i = 0; i < ((int) (Math.random() * 10)); i++) {
            if (TextUtils.isEmpty(newslistBean.getPicUrl())) {
                break;
            }
            mUrls.add(newslistBean.getPicUrl());
        }
        viewHolder.ninePictureLayout.setDataInfos(mUrls);

    }

    @Override
    public int getItemCount() {
        return mNewslistBeans.size();

    }

    static class RecommendedViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvTopic;
        private TextView tvContent;
        private ImageView ivAvatarUrl;
        private NineGridPictureLayout ninePictureLayout;

        public RecommendedViewHolder(@NonNull View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvTopic = itemView.findViewById(R.id.tvTopic);
            tvName = itemView.findViewById(R.id.tvUserName);
            ivAvatarUrl = itemView.findViewById(R.id.ivUserImage);
            ninePictureLayout = itemView.findViewById(R.id.ninePictureLayout);
//            for (int i = 0; i < 8; i++) {
//                ImageView imageView = new RoundImageView(ninePictureLayout.getContext());
//                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//                imageView.setMaxHeight(800);
//                imageView.setMaxWidth(600);
//                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//                imageView.setLayoutParams(layoutParams);
//                ninePictureLayout.addView(imageView);
//            }
        }


    }

}
